/**
 * @author monirul
 */

goog.require('goog.dom');
goog.require('goog.ui.Button');

var GameDeckView =  Class.create({
	
	initialize : function(){
	},
	
	init : function(o){
		var _this = this;
		if(o['parentComponentId'] != null) this._parentComponentId = o['parentComponentId'];
		//if(o['versionNo'] != null) this._versionNo = o['versionNo'];
		this._versionNo =0;
		if(o['model'] != null) _this._model = o['model'];
		if(o['controller'] != null) this._controller = o['controller'];
		
		
		if(o['elements'] != null){
			_this._elements = o['elements'];
			
			
		}	

	    this._model.gameDeskInitialized.attach(function () {
	        _this.rebuildList();
	    });
	    
	    this._model.newUserJoinedRoom.attach(function (sender,user) {
	    	_this.addNewUserGameDeck(user.user);
	    });
	    this._model.userFinishPlay.attach(function () {
	    	_this._finishPlay();
	    });
	    
	    this._model.userActivated.attach(function (sender,data) {
	    	_this._setActive(data.isActive);
	    });
	   
	},
	
	_setActive:function(isActive){
		if(!isActive) isActive = true;
		this._btnPass.setEnabled(isActive);
		this._btnPass.setVisible(isActive);
	},
	
	_finishPlay:function(){
		this._btnPass.setEnabled(false);
	},
	
	show : function () {
        this.rebuildList();
	},
	
	
	
	rebuildList: function() {
		this._room = this._model.getRoom();
		this._selfUser = this._model.getSelfPlayer();
		this.initOtherUsersDeck();
		this.initSelfUsersDeck();
		this.init_dom(); 
	},
	
	init_dom:function(){
		this._btnGetRole = new goog.ui.Button();
		this._btnGetCharacter = new goog.ui.Button();
		this._btnStartGame = new goog.ui.Button();
		this._btnPass = new goog.ui.Button();
		this._btnGetRole.decorate(goog.dom.$('btnGetRole'));
		this._btnGetCharacter.decorate(goog.dom.$('btnGetCharacter'));
		this._btnStartGame.decorate(goog.dom.$('btnStart'));
		this._btnPass.decorate(goog.dom.$('btnPass'));
	    var _controller = this._controller;
	    var _model = this._model;
	    goog.events.listen(this._btnGetRole, goog.ui.Component.EventType.ACTION,
	        function(e) {
	    		_controller.getRole();
	    });
	    
	    goog.events.listen(this._btnGetCharacter, goog.ui.Component.EventType.ACTION,
		        function(e) {
		    		_controller.getCharacter();
		});
	    
	    goog.events.listen(this._btnStartGame, goog.ui.Component.EventType.ACTION,
		        function(e) {
		    		_controller.startGame();
		    	if(_model._selfPlayerDeckModel.getRole() == 'King'){
		    		this._btnPass.setEnabled(true);
		    	}	
		    	
		});
	    goog.events.listen(this._btnPass, goog.ui.Component.EventType.ACTION,
		        function(e) {
		    		_controller.pass();
		});

		if(this._selfUser.name == this._model.getOwner()){
			//alert("wwww");
			this._btnGetRole.setVisible(true);
			this._btnGetCharacter.setVisible(true);
			this._btnStartGame.setVisible(true);
			
			this._btnGetRole.setEnabled(false);
			this._btnGetCharacter.setEnabled(false);
			this._btnStartGame.setEnabled(false);
			this._btnPass.setEnabled(false);
		}else{
			//alert('not owner');
			this._btnGetRole.setVisible(false);
			this._btnGetCharacter.setVisible(false);
			this._btnStartGame.setVisible(false);
			this._btnPass.setVisible(false);
			
		}
	},
	
	getRole:function(){
		this._controller.getRole();
	},
	
	initSelfUsersDeck:function(){
		/* initialize player deck model to set the data */
		var init = {'parentComponentId': this._componentId,'playerId':1};
		var playerDeckModel = ComponentManager.loadComponent({componentId:'selfPlayerMode_instance',
			jsClassName:'PlayerDeckModel',initData:init});
		
		
		/*set player controller*/
		
		var init = {'model':playerDeckModel};
		var playerController = ComponentManager.loadComponent({componentId:'playerController_instance',
			jsClassName:'PlayerDeckController',initData:init});
		
		/* set player deck view */
		var playerDecktViewElements = {'targetLocation':'playerdeck_1' };
		var init = {'parentComponentId':this.componentId,'model':playerDeckModel,'controller':playerController,'elements':playerDecktViewElements};
		var playerDeckView = ComponentManager.loadComponent({componentId:'selfPlayerDeckView_instance',
			jsClassName:'SelfPlayerDeckView',initData:init});
		
		/* call initialize to display the data*/
		playerDeckModel.inititalizePlayerDesk(this._selfUser);
		this._model.setSelfPlayerDeckModel(playerDeckModel);
	},
	
	initOtherUsersDeck:function(){
		this._userList = this._room.userList;
		this._currentUserIndex = 2;
		var playerDeckModel = null;
		for(var i=0;i<this._userList.length;i++){
			if(this._userList[i].name != this._selfUser.name){
				this.addNewUserGameDeck(this._userList[i]);
			}
		}
	},
	
	
	addNewUserGameDeck:function(user){
		/* initialize player deck model to set the data */
		var init = {'parentComponentId': this._componentId,'playerId':this._currentUserIndex};
		var playerDeckModel = ComponentManager.loadComponent({componentId:'playerModel'+this._currentUserIndex +'_instance',
			jsClassName:'PlayerDeckModel',initData:init});
		
		/* set player deck view */
		var playerDecktViewElements = {'targetLocation':'playerdeck_'+this._currentUserIndex };
		var init = {'parentComponentId':this.componentId,'model':playerDeckModel,'controller':'','elements':playerDecktViewElements};
		
		var playerDeckView = ComponentManager.loadComponent({componentId:'playerDeckView'+this._currentUserIndex +'_instance',
			jsClassName:'PlayerDeckView',initData:init});
		
		/* call initialize to display the data*/
		playerDeckModel.inititalizePlayerDesk(user);
		this._model.addPlayerDeckModel(playerDeckModel);
		this._currentUserIndex++;
	}
	
});
